package messengerclient;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import messengercommon.common.ClientRMI;
import messengercommon.common.ServerRMI;
import messengercommon.dto.Message;
import messengercommon.person.Contact;
import messengercommon.person.Person;

/**
 *
 * @author Samus
 */
public class Client extends UnicastRemoteObject implements ClientRMI {

    private static Client instance;
    private ServerRMI server;

    public static Client getInstance() throws RemoteException {
        if (instance == null) {
            instance = new Client();
        }
        return instance;
    }

    private Client() throws RemoteException {
        try {
            assignServer();
        } catch (RemoteException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void assignServer() throws RemoteException, NotBoundException {
        Registry r = LocateRegistry.getRegistry("localhost", 9999);
        this.server = (ServerRMI) r.lookup(ServerRMI.REBIND_NAME);
    }

    @Override
    public void receiveMessage(Message message) throws RemoteException {
        Controller.getInstance().receiveMessage(message);
    }

    @Override
    public void contactChange() throws RemoteException {
        Controller.getInstance().contactChange();
    }

    @Override
    public void contactList(List<Contact> contactlist) throws RemoteException {
        Controller.getInstance().contactList(contactlist);
    }

    public int autentifyUser(Person person) throws RemoteException {
        return server.autentifyUser(person, this);
    }

    public Person userData(Person person) throws RemoteException {
        return server.userData(person.getUsername());
    }

    @Override
    public void userDataChanged(Person person) throws RemoteException {
        Controller.getInstance().assignPerson(person);
    }

    public void userDisconnect(Person person) throws RemoteException {
        server.userDisconnect(person);
    }

    public void nickChange(Person person) throws RemoteException {
        server.userDataChanged(person);
    }

    public int sendMessage(Message message) throws RemoteException {
        return server.autentifyMessage(message);
    }

    public boolean newUser(Person person) throws RemoteException {
        return server.newUser(person);
    }

    public int addContact(Person person, String nomb) throws RemoteException {
        return server.addContact(person, nomb);
    }

    public void readConList(Person pers) throws RemoteException {
        server.readConList(pers);
    }
    
    public ArrayList<String> readLog() throws RemoteException{
        return server.readLog();
    }
}
